<?php
$g4_path = "../"; 
require_once ($g4_path.'common.php');
$title = "상품관리";
$menu = 3;
require_once ('./admin.head.php');
require_once ('./admin.lib.php');

$tpl = new Template_;
$tpl->template_dir = './tpl';
$tpl->compile_dir = './tpl/compile';

$_sqlWhere = $_makeLink = $keyword = NULL; 
$data = $_tplSetting = array();

if(isset($_GET['cate1']) || isset($_GET['cate2']) || isset($_GET['cate3'])) {
    $cate1 = $_GET['cate1'];
    $cate2 = isset($_GET['cate2']) ? $_GET['cate2'] : NULL;
    $cate3 = isset($_GET['cate3']) ? $_GET['cate3'] : NULL;
  
    $_sqlWhere = _makeSqlWhere($_sqlWhere, 'cate1', $cate1);
    $_sqlWhere = _makeSqlWhere($_sqlWhere, 'cate2', $cate2);
    $_sqlWhere = _makeSqlWhere($_sqlWhere, 'cate3', $cate3);
}

if(isset($_GET['keyword'])) {
    $keyword = $_GET['keyword']; 
    $_flag = FALSE; 
    if(!isset($_GET['isCorrect']) || !$_GET['isCorrect']) {
        $_flag = TRUE;
        $keyword = '%'.$keyword.'%';
    }
    $_sqlWhere = _makeSqlWhere($_sqlWhere, 'name', $keyword, $_flag);
}

$nowPage = isset($_GET['page']) ? $_GET['page'] : 1; 
$perPage = 10; 
$_res = sql_fetch("SELECT count(no) as cnt FROM book".$_sqlWhere);
$totalPage = ceil($_res['cnt']/$perPage);
if(( ($nowPage > $totalPage) && $nowPage > 1) || ($nowPage < 1)) {
    alert("페이지 번호가 잘못되었습니다.");
}
$_sqlLimitPage = ($nowPage-1)*$perPage; 
$_sqlOrder = " ORDER BY no DESC"; 
$_sqlLimit = " LIMIT $_sqlLimitPage, $perPage"; 
$sql = 'SELECT * FROM book'.$_sqlWhere.$_sqlOrder.$_sqlLimit;

require_once ('./admin.head.php');

$_res = sql_query($sql);
while($_ret = sql_fetch_array($_res)) {
    $_ret['name'] = htmlspecialchars(stripslashes($_ret['name'])); 
    $_ret['author'] = htmlspecialchars(stripslashes($_ret['author'])); 
    $_ret['publisher'] = htmlspecialchars(stripslashes($_ret['publisher'])); 
    $data[] = $_ret; 
}

if(!$totalPage) {
    $_tplSetting['nodata'] = TRUE; 
}

$_tplSetting['paging'] = get_paging($perPage,$nowPage,$totalPage,"./bookManage.php?page=",makelink(array('page')));
$tpl->assign('data', $data);
$tpl->assign('set', $_tplSetting);
$tpl->define('basic',"bookManage.html");
$tpl->print_('basic');
include_once('inc/book_category.php'); 

